home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / tagsgen.exe / ASMTAG.C < prev    next >
C/C++ Source or Header  |  1991-11-18  |  20KB  |  559 lines

  1. /*
  2.  EPSHeader
  3.  
  4.    File: asmtag.c
  5.    Author: J. Kercheval
  6.    Created: Sun, 07/14/1991  17:25:26
  7. */
  8. /*
  9.  EPSRevision History
  10.  
  11.    J. Kercheval  Sun, 07/14/1991  20:25:59  creation
  12.    J. Kercheval  Mon, 07/15/1991  22:47:30  finish finite state machine parser
  13.    J. Kercheval  Wed, 07/17/1991  21:35:43  add IsMember() and get_token()
  14.    J. Kercheval  Thu, 07/18/1991  19:57:34  add flags checking
  15.    J. Kercheval  Sun, 07/21/1991  15:58:56  add comment block support
  16.    J. Kercheval  Sat, 07/27/1991  21:16:53  remove public post process support
  17.    J. Kercheval  Sat, 07/27/1991  22:50:49  performance considerations (+10%)
  18.    J. Kercheval  Sat, 08/10/1991  17:48:28  speed up IsMember()
  19.    J. Kercheval  Sat, 08/17/1991  22:50:29  use unique function names (ASM...)
  20.    J. Kercheval  Sun, 08/25/1991  23:52:51  fix bug in ASMSymbolWanted()
  21.    J. Kercheval  Thu, 10/03/1991  12:27:37  fix logic outputting local labels
  22. */
  23.  
  24. #include <stdlib.h>
  25. #include <string.h>
  26.  
  27. #include "asmtag.h"
  28. #include "tagio.h"
  29.  
  30.  
  31. /*
  32.  * The finite state machine allows the following interesting paths
  33.  *
  34.  *    1 - Discard, Parse1, Symbol1
  35.  *    2 - Discard, Parse1, Parse2, Symbol2
  36.  *    3 - Discard, Parse1, Parse2, Define
  37.  *
  38.  * all the important cases follow one of these paths according to MASM/TASM
  39.  * syntax.  The exit state is for finish up routine calls and some paths not
  40.  * covered here are simple error paths and probably result from syntax errors
  41.  */
  42. enum state {
  43.     Discard, Parse1, Parse2, Symbol1, Symbol2, Define, Exit
  44. };
  45.  
  46. typedef enum state State;
  47.  
  48.  
  49. #define COMMENT_CHAR ';'
  50.  
  51. #define SYMBOL_SIZE 15
  52.  
  53.  
  54. /*----------------------------------------------------------------------------
  55.  *
  56.  * The symbol lists represent all the symbols we are interested in either
  57.  * obtaining or ignoring.  The order of some of these token lists is
  58.  * important for determining if ouput should be performed.  If you want to
  59.  * change these lists make sure that flag checking is altered to change the
  60.  * changed order.  The first element of each of these symbol lists is a
  61.  * string containing all the first characters within the symbol list.  This
  62.  * allows faster rejection for IsMember() which is called often.
  63.  *
  64.  ---------------------------------------------------------------------------*/
  65.  
  66. /* symbols which are not significant for this parser */
  67. char ASM_NOP_Sym[][SYMBOL_SIZE] =
  68. {
  69.     "cpbfnwo",                  /* list of starting characters of symbols
  70.                                  * below */
  71.     "c",                        /* C language declaration */
  72.     "pascal",                   /* PASCAL language declaration */
  73.     "basic",                    /* BASIC language declaration */
  74.     "fortran",                  /* FORTRAN language declaration */
  75.     "prolog",                   /* PROLOG language declaration */
  76.     "nolanguage",               /* generic language declaration */
  77.     "windows",                  /* WINDOWS exit and entry modifier */
  78.     "oddnear",                  /* overlay modifier */
  79.     "oddfar",                   /* overlay modifier */
  80.     "normal",                   /* normal procedure entry/exit code */
  81.     "\0"
  82. };
  83.  
  84. /* symbols which begin a comment block */
  85. char ASM_comment_block[][SYMBOL_SIZE] =
  86. {
  87.     "c",                        /* list of starting characters of symbols
  88.                                  * below */
  89.     "comment",                  /* begin comment block, next character is
  90.                                  * delimiter */
  91.     "\0"
  92. };
  93.  
  94.  
  95. /* create the function for determining if a character is a delimiter */
  96. #define IsDelim(c) ( _ASM_delim_table[c] )
  97.  
  98. /* the indexed table for white space character lookup */
  99. BOOLEAN _ASM_delim_table[256];
  100.  
  101. /* valid delimiters for this syntax */
  102. char ASM_delim[] = " \t;:=.,\"()<>[]*-+/";
  103.  
  104.  
  105. /* create the function for determining if a character is a whitespace */
  106. #define IsWhite(c) ( _ASM_white_table[c] )
  107.  
  108. /* the indexed table for white space character lookup */
  109. BOOLEAN _ASM_white_table[256];
  110.  
  111. /* whitespace characters */
  112. char ASM_white[] = " \t\v\f";
  113.  
  114.  
  115. /* symbols which both are delimiters and a special token, these are
  116.     special tokens only when found at the the beginning of a string of
  117.     1 or more delimiters */
  118. char ASM_delim_Sym[] = "=:";
  119.  
  120. /* symbols which fit into the Define state and represent a tagged symbol */
  121. /* state Define depends on the token ":" being at index 1 in this list */
  122. char ASM_def[][SYMBOL_SIZE] =
  123. {
  124.     ":e=cd",                    /* list of starting characters of symbols
  125.                                  * below */
  126.     ":",                        /* local labels */
  127.     "equ",                      /* equivalence */
  128.     "=",                        /* equivalence */
  129.     "catstr",                   /* concatenated and named strings */
  130.     "db",                       /* named byte data definition */
  131.     "dw",                       /* named word data definition */
  132.     "dd",                       /* named double word data definition */
  133.     "dp",                       /* named 6 byte far pointer data area
  134.                                  * definition */
  135.     "df",                       /* named 6 byte far pointer definition */
  136.     "dq",                       /* named quad word data definition */
  137.     "dt",                       /* named 10 byte data area */
  138.     "\0"
  139. };
  140.  
  141. /* symbols which fit into the Symbol state and represent a tagged symbol */
  142. char ASM_sym[][SYMBOL_SIZE] =
  143. {
  144.     "pmlsu",                    /* list of starting character of symbols
  145.                                  * below */
  146.     "proc",                     /* procedures */
  147.     "macro",                    /* macros */
  148.     "label",                    /* local labels */
  149.     "struc",                    /* structures */
  150.     "union",                    /* unions */
  151.     "\0"
  152. };
  153.  
  154.  
  155. /*----------------------------------------------------------------------------
  156.  *
  157.  * ASMParserInit() initializes the tables required by the parser The tables
  158.  * used are a simple boolean index which are true if the character
  159.  * corresponding to the index is a member of the associated table.
  160.  *
  161.  ---------------------------------------------------------------------------*/
  162.  
  163. void ASMParserInit()
  164. {
  165.     char *s;
  166.     int i;
  167.  
  168.     /* init the entire block to FALSE */
  169.     for (i = 0; i < 256; i++) {
  170.         _ASM_delim_table[i] = FALSE;
  171.         _ASM_white_table[i] = FALSE;
  172.     }
  173.  
  174.     /* set the characters in the delim set to TRUE */
  175.     for (s = ASM_delim; *s; s++) {
  176.         _ASM_delim_table[*s] = TRUE;
  177.     }
  178.  
  179.     /* NULL is also a delimiter */
  180.     _ASM_delim_table['\0'] = TRUE;
  181.  
  182.     /* set the characters in the white set to TRUE */
  183.     for (s = ASM_white; *s; s++) {
  184.         _ASM_white_table[*s] = TRUE;
  185.     }
  186. }
  187.  
  188. /*----------------------------------------------------------------------------
  189.  *
  190.  * ASMSymbolWanted() returns true if the index into the sym token list is one
  191.  * of the wanted symbols according to the flags list.  The indexes belong
  192.  * with the following symbols and flags:
  193.  *
  194.  *          Flag       Symbol   Index
  195.  *          ---------  -------  -----
  196.  *          flags->af  "proc"   1
  197.  *          flags->am  "macro"  2
  198.  *          flags->al  "label"  3
  199.  *          flags->as  "struc"  4
  200.  *          flags->au  "union"  5
  201.  *
  202.  ---------------------------------------------------------------------------*/
  203.  
  204. BOOLEAN ASMSymbolWanted(Flags * flags, int index)
  205. {
  206.     /* return true if the associated flag is true */
  207.     switch (index) {
  208.             case 1:
  209.             return flags->af;
  210.             break;
  211.         case 2:
  212.             return flags->am;
  213.             break;
  214.         case 3:
  215.             return flags->al;
  216.             break;
  217.         case 4:
  218.             return flags->as;
  219.             break;
  220.         case 5:
  221.             return flags->au;
  222.             break;
  223.         default:
  224.             return FALSE;
  225.             break;
  226.     }
  227. }
  228.  
  229.  
  230. /*----------------------------------------------------------------------------
  231.  *
  232.  * ASMIsMember() takes the token passed and check for membership in the null